package com.mtiiot.fstask.esdao;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Map;

/**
 * Created by Administrator on 16-7-29.
 */
@Repository
public class BaseElasticsearch {
    @Autowired
    private ElasticsearchDataSource esDataSource;
    public ElasticsearchDataSource getEsDataSource() {
        return esDataSource;
    }
    public void setEsDataSource(ElasticsearchDataSource esDataSource) {
        this.esDataSource = esDataSource;
    }

    /**
     * 系统自动产生id 创建1个文档
     * @param index 索引名称
     * @param type  索引type
     * @param sourcecontent 要索引的内容
     */
    public boolean createIndex(String index,String type,String sourcecontent) throws Exception {
        Client client=null;
        try{
            client=esDataSource.getClient();
            IndexResponse response = client.prepareIndex(index, type).setSource(sourcecontent).execute().actionGet();
            return response.isCreated();
        }finally {
            if(client!=null){
                esDataSource.releaseClient(client);
            }
        }
    }

    /**
     * 自己设置id创建1个文档
     * @param index 索引名称
     * @param type  索引type
     * @param sourcecontent 要索引的内容
     */
    public boolean createIndexById(String index,String type,String sourcecontent,String id) throws Exception {
        Client client=null;
        try{
            client=esDataSource.getClient();
            IndexResponse response = client.prepareIndex(index, type).setId(id).setSource(sourcecontent).execute().actionGet();
            return response.isCreated();
        }finally {
            if(client!=null){
                esDataSource.releaseClient(client);
            }
        }
    }

    /**
     * 根据修改文档
     * @param index 索引名称
     * @param type  索引type
     * @param id  id
     * @param map 要索引的内容
     */
    public boolean updateIndex(String index,String type,String id,Map map) throws Exception {
        Client client=null;
        try{
            client=esDataSource.getClient();
            UpdateResponse response = client.prepareUpdate(index, type,id).setDoc(map).execute().actionGet();
            return response.isCreated();
        }finally {
            if(client!=null){
                esDataSource.releaseClient(client);
            }
        }
    }


    /**
     * 根据id删除文档
     * @param index
     * @param type
     * @param id
     */
    public boolean deleteIndex(String index, String type, String id) throws Exception {
        Client client=null;
        try{
            client=esDataSource.getClient();
            DeleteResponse response = client.prepareDelete(index, type, id)
                    .execute()
                    .actionGet();
            return response.isFound();
        }finally {
            if(client!=null){
                esDataSource.releaseClient(client);
            }
        }
    }

    /**
     * 删除多个文档
     * @param index
     * @param type
     * @param ids 多个id逗号隔开
     */
    public boolean deleteIndexs(String index, String type, String ids) throws Exception {
        Client client=null;
        try{
            client=esDataSource.getClient();
            String[] ss=ids.split(",");
            BulkRequestBuilder brb=client.prepareBulk();
            for(String id:ss){
                brb.add(client.prepareDelete(index, type, id));
            }
            BulkResponse br=brb.execute().actionGet();
            if(br.hasFailures()){
                return false;
            }
            return true;
        }finally {
            if(client!=null){
                esDataSource.releaseClient(client);
            }
        }
    }




}
